#include "serviceview_console_impl.h"
#include "service_impl.h"
#include "tools.h"
#include <iostream>
#include <strings.h>

using namespace std;
using namespace MAN;

ServiceViewConsoleImpl::ServiceViewConsoleImpl()
{
	m_pService = new ServiceImpl;
}

//显示运营管理子菜单
void ServiceViewConsoleImpl:: menu(void )
{
	int choice = 0;
	while(1)
	{
		do
		{
			system("clear");
			puts("╭════════════════════════════════════════════════════╮");  
   			puts("│              　　企业业务子系统      　    　 　   │");  
		    puts("╰════■□■□══════════════════════════════════■□■□══════╯"); 
		    puts("");
			cout << "		  1、增加部门" << endl;
			cout << "		  2、删除部门" << endl;
			cout << "		  3、列出所有部门" << endl;
			cout << "		  4、增加员工" << endl;
			cout << "		  5、删除员工" << endl;
			cout << "		  6、修改员工信息" << endl;
			cout << "		  7、列出部门员工" << endl;
			cout << "		  8、列出所有员工" << endl;
			cout << "		  9、返回上一层" << endl;
			puts("");
			puts("╭════════════════════════════════════════════════════╮"); 
			puts("╰════■□■□══════════════════════════════════■□■□══════╯");
			puts(""); 
			cout << "		  请选择:";
						
			cin >> choice ;
		}while(choice < 1 || choice >=10);
		system("clear");
		switch(choice)
		{
			case 1: addDept();  	break;
			case 2: deleteDept();	break;
			case 3: listDept(); 	break;
			case 4: addEmp(); 		break;
			case 5: deleteEmp(); 	break;
			case 6: modifyEmp(); 	break;
			case 7: listEmp(); 		break;
			case 8: listAllEmp(); 	break;
			case 9: return;	
		}
		clear_stdin();
		anykey_continue();
	}
}

//处理增加部门菜单项
void ServiceViewConsoleImpl:: addDept(void )
{
	char buff[255] = {};
	cout << "请输入预添加部门名称：" << endl;
	cin >> buff;
	clear_stdin();
	
	int DeptId = generator_DeptId(DeptId_PATH);
	Department d(buff,DeptId);

	if( m_pService->addDept(d))
	{
		cout << "添加部门成功 ！" << endl;
		cout << "部门名称: " << buff << endl;
		cout << "部门ID：" << DeptId << endl;
	}
	else
	{
		cout << "添加部门失败 ！" <<endl;
		cout << "部门已经存在　!" << endl;
	}
}

//处理删除部门菜单项 
void ServiceViewConsoleImpl:: deleteDept(void )
{
	int deptid = 0;
	cout << "请输入删除部门Id：" ;
	cin >> deptid;
	clear_stdin();
	cout << "确认删除部门 y/n ! ";

	if( getchar() == 'y' )
	{
		if(m_pService -> deleteDept(deptid))
		{
			cout << "删除部门成功 ！"<< endl;
		}
		else
		{
			cout << "部门ID不存在 ！"<<endl;
		}

	}
	else
	{
		cout << "删除部门失败 ！" <<endl;
	}

}

//处理列出部门菜单项
void ServiceViewConsoleImpl:: listDept(void )
{

	vector<Department> temp_vec = m_pService -> listDept();
	clear_stdin();
	cout << "*************************************************" << endl;
	for(unsigned int i= 0; i < temp_vec.size();i++)
	{
		cout << "部门ID：" << temp_vec[i].m_nId << "　部门名称: "<<temp_vec[i]<< endl;
	}
	cout << "*************************************************" << endl;

}

//处理增加员工菜单项
void ServiceViewConsoleImpl:: addEmp(void )
{
	char buff1[50] = {};
	char buff2[20] = {};
	int depid;
	bool flag;
	int age;
	int EmpID = generator_EmpId(EmpID_PATH);
	cout << "请输入部门ID：";
	cin >> depid;
	clear_stdin();

	cout << "请输入姓名：";
	cin >> buff1;
	clear_stdin();

	int flag_time = 0;
	while(1)
	{
		
		if(flag_time == 0)
		{
			cout << "请输入性别(男性：true  女性：false): ";
			cin >> buff2;
			clear_stdin();
			flag_time++;
		}
		else
		{
			cout << "请重新输入性别(男性：true  女性：false): ";
			bzero(buff2,sizeof(buff2));
			cin >> buff2;
			clear_stdin();
			flag_time++;
		}
		if(strcmp(buff2,"true") == 0)
		{
			flag = true;
			break;
		}
		else if(strcmp(buff2,"false") == 0)
		{
			flag = false;
			break;
		}
	}
	
	cout << "请输入年龄: ";
	cin >> age;
	clear_stdin();

	Employee e(buff1,flag,age,EmpID);

	if( m_pService->addEmp(e,depid) )
	{
		cout << "添加员工成功 ！" << endl;
		cout << "员工ID: " << EmpID <<endl;
		return ;
	}
	else
	{
		cout << "添加员工失败 ！" << endl;
		return ;
	}
}

//处理删除员工菜单项
void ServiceViewConsoleImpl:: deleteEmp(void )
{
	int depid;
	int empid;
	cout << "请输入部门ID：";
	cin >> depid;
	clear_stdin();
	
	cout << "请输入员工ID: ";
	cin >> empid;
	clear_stdin();

	if(m_pService->deleteEmp(depid,empid))
	{
		cout << "删除员工成功　！" << endl;
		return;
	}
	else
	{
		cout << "员工ID错误 / 部门ID错误　！" << endl;
		return ;
	}	
			
}

//处理修改员工信息菜单项  
void ServiceViewConsoleImpl:: modifyEmp(void )
{
	char buff1[50] = {}; //name
	char buff2[20] = {};
	int depid;
	bool flag;
	int age;
	int EmpID;
	cout << "请输入部门ID：";
	cin >> depid;
	clear_stdin();
	cout << "请输入员工ID :";
	cin >> EmpID;
	clear_stdin();

	cout << "请输入姓名：";
	cin >> buff1;
	clear_stdin();
	int flag_time = 0;
	while(1)
	{

		if(flag_time == 0)
		{
			cout << "请输入性别(男性：true  女性：false): ";
			cin >> buff2;
			clear_stdin();
			flag_time++;
		}
		else
		{
			cout << "请重新输入性别(男性：true  女性：false): ";
			bzero(buff2,sizeof(buff2));
			cin >> buff2;
			clear_stdin();
			flag_time++;
		}
		if(strcmp(buff2,"true") == 0)
		{
			flag = true;
			break;
		}
		else if(strcmp(buff2,"false") == 0)
		{
			flag = false;
			break;
		}
	}
	cout << "请输入年龄: ";
	cin >> age;
	clear_stdin();
	

	Employee e(buff1,flag,age,EmpID);

	if( m_pService->modifyEmp(e,depid) )
	{
		cout <<  "修改员工信息成功 ！" << endl;//重载
		return ;
	}
	else
	{
		cout << "修改员工信息失败 ！" << endl;
		cout << "员工ID错误 / 部门ID错误　！";
		return ;
	}

	
}

//处理列出部门员工菜单项 
void ServiceViewConsoleImpl:: listEmp(void )
{
	int depid;
	cout << "请输入部门ID：";
	cin >> depid;
	clear_stdin();
	vector<Employee> temp_emp = m_pService->listEmp(depid);

	cout << "*********************************************" << endl;
	for (unsigned int i = 0; i < temp_emp.size(); ++i)
	{
		cout << temp_emp[i] <<endl;
	}

	cout << "*********************************************" << endl;
	
}

//处理列出所有部门员工菜单项 
void ServiceViewConsoleImpl:: listAllEmp(void )
{
	vector<Employee> temp_allemp = m_pService ->listAllEmp();
	cout << "*********************************************" << endl;
	for(unsigned int i = 0; i < temp_allemp.size(); ++i)
	{
		cout << temp_allemp[i] << endl;
	}
	cout << "*********************************************" << endl;
}

ServiceViewConsoleImpl::~ServiceViewConsoleImpl()
{
	delete m_pService;
}


